package com.ruoyi.personnelMatters.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.compensationManagement.service.ICompensationSalarySettingService;
import com.ruoyi.compensationManagement.service.impl.CompensationSalarySettingServiceImpl;
import com.ruoyi.personnelMatters.domain.UserListVo;
import com.ruoyi.system.mapper.SysCommonMapper;
import com.ruoyi.system.service.ISysUserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.personnelMatters.mapper.PersonAssessmentMapper;
import com.ruoyi.personnelMatters.domain.PersonAssessment;
import com.ruoyi.personnelMatters.service.IPersonAssessmentService;

/**
 * 人事考核Service业务层处理
 *
 * @author ruoyi
 * @date 2022-09-16
 */
@Service
public class PersonAssessmentServiceImpl implements IPersonAssessmentService {
    @Autowired
    private PersonAssessmentMapper personAssessmentMapper;

    @Autowired
    private SysCommonMapper sysCommonMapper;

    @Autowired
    private ICompensationSalarySettingService compensationSalarySettingService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询人事考核
     *
     * @param personAssessmentId 人事考核主键
     * @return 人事考核
     */
    @Override
    public PersonAssessment selectPersonAssessmentByPersonAssessmentId(Long personAssessmentId) {
        return personAssessmentMapper.selectPersonAssessmentByPersonAssessmentId(personAssessmentId);
    }

    /**
     * 查询人事考核列表
     *
     * @param personAssessment 人事考核
     * @return 人事考核
     */
    @Override
    public List<PersonAssessment> selectPersonAssessmentList(PersonAssessment personAssessment) {
        return personAssessmentMapper.selectPersonAssessmentList(personAssessment);
    }

    /**
     * 新增人事考核
     *
     * @param personAssessment 人事考核
     * @return 结果
     */
    @Override
    public int insertPersonAssessment(PersonAssessment personAssessment) {
        return personAssessmentMapper.insertPersonAssessment(personAssessment);
    }

    /**
     * 审批人事考核
     *
     * @param personAssessment 人事考核
     * @return 结果
     */
    @Override
    public int updatePersonAssessment(PersonAssessment personAssessment) {
        String result = personAssessment.getAssessmentResult();
        Map<String, Long> map = personnelAssessmentJudgment(personAssessment.getUserId(), result);
        if (result.equals("1") || result.equals("2") || result.equals("3")) {
            if (map.get("newPayId") != 0L) {
                SysUser sysUser = new SysUser();
                sysUser.setUserId(personAssessment.getUserId());
                sysUser.setSalaryScale(map.get("newPayId"));
                sysUserService.updateUserSalaryScale(sysUser);
                personAssessment.setAssessmentResultInfo("增加一级薪级");
                personAssessment.setOldPayId(map.get("oldPayId"));
                personAssessment.setOldPayLevel(compensationSalarySettingService.selectCompensationSalarySettingByPayId(map.get("oldPayId")).getPayLevel());
                personAssessment.setNewPayId(map.get("newPayId"));
                personAssessment.setNewPayLevel(compensationSalarySettingService.selectCompensationSalarySettingByPayId(map.get("newPayId")).getPayLevel());
            } else {
                personAssessment.setAssessmentResultInfo("薪级达到上限，无法升级，保持原薪级等级");
                personAssessment.setOldPayId(map.get("oldPayId"));
                personAssessment.setOldPayLevel(compensationSalarySettingService.selectCompensationSalarySettingByPayId(map.get("oldPayId")).getPayLevel());
                personAssessment.setNewPayId(map.get("oldPayId"));
                personAssessment.setNewPayLevel(compensationSalarySettingService.selectCompensationSalarySettingByPayId(map.get("oldPayId")).getPayLevel());
            }
        } else if (result.equals("4") || result.equals("5")) {
            personAssessment.setAssessmentResultInfo("薪级不变");
            personAssessment.setOldPayId(map.get("oldPayId"));
            personAssessment.setOldPayLevel(compensationSalarySettingService.selectCompensationSalarySettingByPayId(map.get("oldPayId")).getPayLevel());
            personAssessment.setNewPayId(map.get("oldPayId"));
            personAssessment.setNewPayLevel(compensationSalarySettingService.selectCompensationSalarySettingByPayId(map.get("oldPayId")).getPayLevel());
        }
        personAssessment.setAssessmentUserId(SecurityUtils.getUserId());
        personAssessment.setAssessmentUserName(sysCommonMapper.getUserName(SecurityUtils.getUserId()));
        personAssessment.setAssessmentAgreeTime(new Date());
        return personAssessmentMapper.updatePersonAssessment(personAssessment);
    }

    /**
     * 批量删除人事考核
     *
     * @param personAssessmentIds 需要删除的人事考核主键
     * @return 结果
     */
    @Override
    public int deletePersonAssessmentByPersonAssessmentIds(Long[] personAssessmentIds) {
        return personAssessmentMapper.deletePersonAssessmentByPersonAssessmentIds(personAssessmentIds);
    }

    /**
     * 删除人事考核信息
     *
     * @param personAssessmentId 人事考核主键
     * @return 结果
     */
    @Override
    public int deletePersonAssessmentByPersonAssessmentId(Long personAssessmentId) {
        return personAssessmentMapper.deletePersonAssessmentByPersonAssessmentId(personAssessmentId);
    }

    /**
     * 生成当月每个员工年度考核表
     */
    @Override
    public int generateAppraisalForm() {
        //查在职员工信息
        int num = 0;
        List<UserListVo> userInfoList = sysCommonMapper.getUserInfoList();
        for (UserListVo userListVo : userInfoList) {
            if (userListVo.getUserId() != 1) {
                PersonAssessment personAssessment = new PersonAssessment();
                personAssessment.setUserId(userListVo.getUserId());
                personAssessment.setNickName(userListVo.getNickName());
                personAssessment.setUserIdcardnum(userListVo.getIdCardNum());
                personAssessment.setAssessmentTime(new Date());
                personAssessmentMapper.insertPersonAssessment(personAssessment);
                num++;
            }
        }
        return num;
    }

    /**
     * 查询个人考核详情
     */
    @Override
    public PersonAssessment selectPersonAssessmentByPersonAssessmentIdAndUid(Long personAssessmentId, Long userId) {
        return personAssessmentMapper.selectPersonAssessmentByPersonAssessmentIdAndUid(personAssessmentId, userId);
    }

    /**
     * 根据考核返回对应薪级
     * 优秀/合格/基本合格  薪级+1；不合格/不定等次，薪级不变
     *
     * @param userId
     * @param assessmentResult
     * @return
     */
    @Override
    public Map<String, Long> personnelAssessmentJudgment(Long userId, String assessmentResult) {
        HashMap<String, Long> map = new HashMap<>();
        SysUser user = sysCommonMapper.getSysUserByUserId(userId);
        Long salaryScale = user.getSalaryScale();
        //薪级+1
        Long upPayScale = 0L;
        if (assessmentResult.equals("1") || assessmentResult.equals("2") || assessmentResult.equals("3")) {
            map.put("oldPayId", salaryScale);
            upPayScale = compensationSalarySettingService.getUpPayScale(salaryScale);
            map.put("newPayId", upPayScale);
        }
        //薪级不变
        else if (assessmentResult.equals("4") || assessmentResult.equals("5")) {
            map.put("oldPayId", salaryScale);
            map.put("newPayId", salaryScale);
        }
        return map;
    }
}
